//void, Obj This

Settlement this, sett_target;
Building bld;	
int min_resource_food, min_resource_gold, i, p;
ObjList olUnitsTributeOut, sel;   
Wagon wagon;
str target_name;

Sleep(rand(1000) + 1000);
this = This.AsBuilding().settlement;
bld = This.AsBuilding();

if(EnvReadInt(this, "AssignName") == 0){
	Building build;
	str names;
	
	EnvWriteInt(this, "AssignName", 1);
	if(bld.name == ""){
		names = "B_"+bld.class+"_"+bld.player+bld.race+rand(99)+rand(99);
		build = GetNamedObj(names).obj.AsBuilding;
		if(!build.IsValid){
			bld.SetName("");       
			bld.SetName(names);
		} else {
			names = "B_"+bld.class+"_"+bld.player+bld.race+rand(99)+rand(99);
			bld.SetName("");       
			bld.SetName(names);
		}  
	}
}

p = .player;              
min_resource_food = GetConst("MinResQtyToTransportFood");
min_resource_gold = GetConst("MinResQtyToTransportGold");

while (1) {

	Sleep(1000);
	if(p != .player){
		EnvWriteString(this, "tribute_food", "off");
		EnvWriteString(this, "tribute_gold", "off");
		EnvWriteString(this, "tribute_people", "off");
								
		EnvWriteString(this, "stoptribute_food", "on");
		EnvWriteString(this, "stoptribute_gold", "on");
		EnvWriteString(this, "stoptribute_people", "on");
		.StopSupply();
		break;
	}

	if (.supplied == this)
		.StopSupply();
	//if (!.supplied.IsValid()) continue;

	if(EnvReadString(this, "tribute_gold") == "on"){

		target_name = EnvReadString(this, "tribute_gold_target");
		sett_target = GetNamedObj(target_name).obj.AsBuilding.settlement;
		if(sett_target.IsValid){
			if(.IsEnemy(sett_target.player)) {
				EnvWriteString(this, "tribute_gold", "off");
				EnvWriteString(this, "stoptribute_gold", "on");
				continue;
			}
			if(.gold > min_resource_gold) {
				wagon = .CreateMuleGold(1000);
				if(wagon.IsValid){
					wagon.SetCommand("unload", sett_target.GetCentralBuilding());
					wagon.AddCommand(false, "unload", .GetCentralBuilding());
				}
			}
			sel = _GetSelection().ObjAlly(.player).ObjClass("IronMine");
			if(sel.count == 1){
				Building tw;
				
				tw = sel[0].AsBuilding;
				if(tw.IsValid){
					if(tw == .GetCentralBuilding){
						//UserNotification("tribute people", "", Point(target_x, target_y), .player); 
						UserNotification("tribute gold", "", sett_target.GetCentralBuilding.pos, .player); 
						//UserNotification("tribute food", "", sett_target.GetCentralBuilding.pos, .player); 
					}
				}
			}
		}
	}

	Sleep(1000);
	
	if(EnvReadString(this, "tribute_food") == "on"){
					
		target_name = EnvReadString(this, "tribute_food_target");
		sett_target = GetNamedObj(target_name).obj.AsBuilding.settlement;
		if(sett_target.IsValid){
			if(.IsEnemy(sett_target.player)) {
				EnvWriteString(this, "tribute_food", "off");
				EnvWriteString(this, "stoptribute_food", "on");
				.StopSupply();
				continue;
			}
			if (.food > min_resource_food) {
				int r;
		
				r = .food - min_resource_food;
				wagon = .CreateMuleFood(r);
				if(wagon.IsValid){
					wagon.SetCommand("unload", sett_target.GetCentralBuilding());
					wagon.AddCommand(false, "unload", .GetCentralBuilding()); 
				}
			}
			sel = _GetSelection().ObjAlly(.player).ObjClass("IronMine");
			if(sel.count == 1){
				Building tw;
				
				tw = sel[0].AsBuilding;
				if(tw.IsValid){
					if(tw == .GetCentralBuilding){
						//UserNotification("tribute people", "", Point(target_x, target_y), .player); 
						//UserNotification("tribute gold", "", Point(target_x, target_y), .player); 
						UserNotification("tribute food", "", sett_target.GetCentralBuilding.pos, .player); 
					}
				}
			}
		}
	}
	
	Sleep(1000);
	
	if(EnvReadString(this, "tribute_people") == "on"){
		olUnitsTributeOut.Clear;
		if(.Units.count > 0) olUnitsTributeOut = .Units.ObjClass("Military");
					
		{
			int target_x, target_y;
			point pos, pp;
			rect rc;
			
			target_name = EnvReadString(this, "tribute_people_target");
			sett_target = GetNamedObj(target_name).obj.AsBuilding.settlement;                        
			target_x = EnvReadInt(this, "tribute_people_target_x");
			target_y = EnvReadInt(this, "tribute_people_target_y");
			if(sett_target.IsValid){
				Building bld_target;
				bld_target = sett_target.GetCentralBuilding;
				if(!.IsEnemy(sett_target.player)){
					if(olUnitsTributeOut.count >= 1){
						if(bld_target.IsHeirOf("Outpost") ||
						   bld_target.IsHeirOf("BaseShipyard") ||
						   bld_target.IsHeirOf("IronMine") ||
						   bld_target.IsHeirOf("BaseTownhall")){
						   
							olUnitsTributeOut.SetCommand("enter", bld_target);
								
						} else {
							rc = GetMapRect();
							pos.Set(0, bld_target.sight / 3);
							pos.Rot(rand(360));     pp = pos + bld_target.pos; pp.IntoRect(rc); 
							olUnitsTributeOut.SetCommand("move", pp);
						}
						//Sleep(4*1000);
					}
				} else {
					EnvWriteString(this, "tribute_people", "off");
					EnvWriteString(this, "stoptribute_people", "on");
				}
				sel = _GetSelection().ObjAlly(.player).ObjClass("IronMine");
				if(sel.count == 1){
					Building tw;
					
					tw = sel[0].AsBuilding;
					if(tw.IsValid){
						if(tw == .GetCentralBuilding){
							UserNotification("tribute people", "", bld_target.pos, .player); 
							//UserNotification("tribute gold", "", Point(target_x, target_y), .player); 
							//UserNotification("tribute food", "", Point(target_x, target_y), .player); 
						}
					}
				}
			} else if(target_x != 0 && target_y != 0){
				if(olUnitsTributeOut.count >= 1){
					rc = GetMapRect();
					pos.Set(0, 200);
					pos.Rot(rand(360));     pp = pos + Point(target_x, target_y); pp.IntoRect(rc); 
					olUnitsTributeOut.SetCommand("move", pp);

					//Sleep(4*1000);
				}
				sel = _GetSelection().ObjAlly(.player).ObjClass("IronMine");
				if(sel.count == 1){
					Building tw;
					
					tw = sel[0].AsBuilding;
					if(tw.IsValid){
						if(tw == .GetCentralBuilding){
							UserNotification("tribute people", "", Point(target_x, target_y), .player); 
							//UserNotification("tribute gold", "", Point(target_x, target_y), .player); 
							//UserNotification("tribute food", "", Point(target_x, target_y), .player); 
						}
					}
				}
			}
		}
	}       
}